package cn.getech.data.development.controller.realAlarm;


import cn.getech.data.development.constant.Constant;
import cn.getech.data.development.constant.RealTaskAlarmModeEnum;
import cn.getech.data.development.constant.RunStatus;
import cn.getech.data.development.dto.IdAndNameDto;
import cn.getech.data.development.model.dto.RealTaskAlarmSaveDto;
import cn.getech.data.development.model.vo.RealTaskAlarmSearchVO;
import cn.getech.data.development.service.IBdpRealTaskAlarmService;
import cn.getech.data.intelligence.common.utils.R;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import cn.getech.system.center.constant.module.ModuleEnum;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 实时任务告警表 前端控制器
 * </p>
 *
 * @author hujz
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/bdpRealTaskAlarm")
public class BdpRealTaskAlarmController {

    @Autowired
    private IBdpRealTaskAlarmService iBdpRealTaskAlarmService;

    @ApiOperation("获取实时告警分页列表")
    @PostMapping("/page")
    public R page(@RequestBody RealTaskAlarmSearchVO realTaskAlarmSearchVO) {
        return R.okWithPage(iBdpRealTaskAlarmService.alarmPage(realTaskAlarmSearchVO));
    }

    @ApiOperation("删除实时告警信息")
    @GetMapping("/delete/{id}")
    @SysLog(value = "删除实时告警信息", type = OperationTypeEnum.DELETECODE, modulesType = OperationModulesEnum.SYSMODECODE, platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R delete(@PathVariable Integer id) {
        iBdpRealTaskAlarmService.delete(id);
        return R.ok();
    }

    @ApiOperation("启用/禁用实时告警信息")
    @GetMapping("/startUsing/{id}/{state}")
    @SysLog(value = "启用/禁用实时告警信息", type = OperationTypeEnum.OPENCLOSECODE, modulesType = OperationModulesEnum.SYSMODECODE, platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R startUsing(@PathVariable("id") Integer id,
                        @PathVariable("state") Boolean state) {
        Integer falge = cn.getech.data.intelligence.common.utils.Constant.StateEnum.DISABLE.getValue();
        if(state){
            falge = cn.getech.data.intelligence.common.utils.Constant.StateEnum.ENABLE.getValue();
        }
        iBdpRealTaskAlarmService.updateStatusById(id, falge);
        return R.ok();
    }

    @ApiOperation("新建是需要获取的实时告警信息的数据")
    @GetMapping("/needInfo")
    public R needInfo() {
        return R.okWithData(iBdpRealTaskAlarmService.needInfo());
    }

    @ApiOperation("创建实时告警信息")
    @PostMapping("/save")
    @SysLog(value = "创建实时告警信息", type = OperationTypeEnum.ADDCODE, modulesType = OperationModulesEnum.SYSMODECODE, platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R save(@RequestBody RealTaskAlarmSaveDto realTaskAlarmSaveDto) {
        iBdpRealTaskAlarmService.save(realTaskAlarmSaveDto);
        return R.ok();
    }

    @ApiOperation("获取实时告警信息详情")
    @GetMapping("/info/{id}")
    @SysLog(value = "获取实时告警信息详情", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE, platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R getInfoById(@PathVariable("id") Integer id) {
        return R.okWithData(iBdpRealTaskAlarmService.getInfoById(id));
    }

    @ApiOperation("编辑实时告警信息")
    @PostMapping("/update")
    @SysLog(value = "编辑实时告警信息", type = OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.SYSMODECODE, platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R update(@RequestBody RealTaskAlarmSaveDto realTaskAlarmSaveDto) {
        iBdpRealTaskAlarmService.update(realTaskAlarmSaveDto);
        return R.ok();
    }


    @ApiOperation("获取实时任务的类型")
    @GetMapping("/realTaskType")
    public R realTaskType() {
        List<JSONObject> data = new ArrayList<>();
        for (Constant.RealTimeTaskType e : Constant.RealTimeTaskType.values()) {
            JSONObject obj = new JSONObject();
            obj.put("id", e.getCode());
            obj.put("name", e.getName());
            data.add(obj);
        }
        return R.okWithData(data);
    }

    @ApiOperation("获取实时任务告警的触发的条件")
    @GetMapping("/realTaskCondition")
    public R realTaskCondition() {
        List<JSONObject> data = new ArrayList<>();
        for (RealTaskAlarmModeEnum e : RealTaskAlarmModeEnum.values()) {
            JSONObject obj = new JSONObject();
            obj.put("id", e.getCode());
            obj.put("name", e.getName());
            data.add(obj);
        }
        return R.okWithData(data);
    }

    //获取全部接收人列表
    @ApiOperation("获取接收人列表")
    @GetMapping("/getReceiver/list")
    //@SysLog(value="接收人列表",type= OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R getReceiverList() {
        List<IdAndNameDto> list = iBdpRealTaskAlarmService.getRecriverList();
        JSONArray array = new JSONArray();
        JSONObject object;
        if(CollectionUtil.isNotEmpty(list)){
            for(IdAndNameDto idAndNameDto:list ){
                object = new JSONObject();
                object.put("userId", idAndNameDto.getId());
                object.put("userName", idAndNameDto.getName());
                array.add(object);
            }
        }
        return R.okWithData(array);
    }


    //获取全部创建人列表
    @ApiOperation("获取创建人列表")
    @GetMapping("/getCreater/list")
    //@SysLog(value="接收人列表",type= OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R getCreaterList() {
        List<IdAndNameDto> list = iBdpRealTaskAlarmService.getCreaterList();
        JSONArray array = new JSONArray();
        JSONObject object;
        if(CollectionUtil.isNotEmpty(list)){
            for(IdAndNameDto idAndNameDto:list ){
                object = new JSONObject();
                object.put("userId", idAndNameDto.getId());
                object.put("userName", idAndNameDto.getName());
                array.add(object);
            }
        }
        return R.okWithData(array);
    }


//    @GetMapping("/test/list")
//    public R test() {
//        iBdpRealTaskAlarmService.cheackIsAlarmAndPushMsg(845, RunStatus.FAIL.getStatus());
//        return R.ok();
//    }


}

